Page({
  data: {
    userInfo: {
      avatarUrl: '',
      nickName: '',
      userId: ''
    },
    reservationStats: {
      total: 0,
      ongoing: 0,
      completed: 0
    },
    recentReservations: []
  },

  onLoad(options) {
    this.loadUserInfo();
    this.loadReservationStats();
    this.loadRecentReservations();
  },

  onShow() {
    // 每次显示页面时刷新数据
    this.loadReservationStats();
    this.loadRecentReservations();
  },

  // 加载用户信息
  loadUserInfo() {
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        userInfo: {
          avatarUrl: userInfo.avatarUrl,
          nickName: userInfo.nickName,
          userId: 'U' + Date.now().toString().slice(-6) // 模拟用户ID
        }
      });
    }
  },

  // 加载预约统计
  loadReservationStats() {
    // 模拟API调用
    // 实际接口: GET /api/user/reservation-stats
    // 返回值: { code: 200, data: { total: 10, ongoing: 2, completed: 8 } }
    
    setTimeout(() => {
      this.setData({
        reservationStats: {
          total: 12,
          ongoing: 3,
          completed: 9
        }
      });
    }, 300);
  },

  // 加载最近预约
  loadRecentReservations() {
    // 模拟API调用
    // 实际接口: GET /api/user/reservations?limit=3
    // 返回值: { code: 200, data: [{ id, venueName, date, timeSlot, status }] }
    
    setTimeout(() => {
      this.setData({
        recentReservations: [
          {
            id: '1',
            venueName: '多功能会议室',
            date: '2025-11-08',
            timeSlot: '14:00-16:00',
            status: 'ongoing',
            statusText: '进行中'
          },
          {
            id: '2',
            venueName: '生涯咨询室',
            date: '2025-11-07',
            timeSlot: '10:00-12:00',
            status: 'ongoing',
            statusText: '待使用'
          },
          {
            id: '3',
            venueName: 'DIY手工坊',
            date: '2025-11-05',
            timeSlot: '18:00-20:00',
            status: 'completed',
            statusText: '已完成'
          }
        ]
      });
    }, 300);
  },

  // 编辑个人信息
  onEditProfile() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  // 查看预约详情
  onViewReservation(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: '查看预约: ' + id,
      icon: 'none'
    });
  },

  // 查看全部预约
  onViewAllReservations() {
    wx.showToast({
      title: '查看全部预约',
      icon: 'none'
    });
  },

  // 取消预约
  onCancelReservation(e) {
    const id = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '取消预约',
      content: '确定要取消此预约吗？',
      confirmText: '确定',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          this.cancelReservation(id);
        }
      }
    });
  },

  // 执行取消预约
  cancelReservation(id) {
    // 模拟API调用
    // 实际接口: DELETE /api/reservations/:id
    // 返回值: { code: 200, message: '取消成功' }
    
    wx.showLoading({ title: '取消中...' });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '取消成功',
        icon: 'success'
      });
      
      // 从列表中移除该预约项
      const recentReservations = this.data.recentReservations.filter(item => item.id !== id);
      this.setData({
        recentReservations
      });
      
      // 更新统计数据
      this.loadReservationStats();
    }, 1000);
  },

  // 查看历史记录
  onViewHistory() {
    wx.showToast({
      title: '历史记录',
      icon: 'none'
    });
  },

  // 设置
  onSettings() {
    wx.showToast({
      title: '设置',
      icon: 'none'
    });
  },

  // 关于
  onAbout() {
    wx.showToast({
      title: '关于',
      icon: 'none'
    });
  },

  // 退出登录
  onLogout() {
    wx.showModal({
      title: '退出登录',
      content: '确定要退出登录吗？',
      confirmText: '确定',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync();
          wx.reLaunch({
            url: '/pages/login/login'
          });
        }
      }
    });
  }
});
